package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.entity.MaterialManagement;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.MaterialManagementService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 物料管理控制层
 * 2020-12-24
 * PurpleStar
 */
@RestController
@RequestMapping("/management")
public class MaterialManagementController extends BaseController {

    @Autowired
    private MaterialManagementService managementService;

    /**
     * 添加物料管理
     *
     * @param params
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody JSONObject params) {
        MaterialManagement management = params.getJSONObject("materialManagement").toJavaObject(MaterialManagement.class);
        managementService.save(management);
        return success("添加成功");
    }

    /**
     * 查看物料管理
     *
     * @param params
     * @return
     */
    @PostMapping("/inquire")
    public Object inquire(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        if (page > 0) {
            Integer page1 = page - 1;
            return buildPage(managementService.select(page1));
        } else {
            return fail("页面加载错误");
        }
    }

    /**
     * 查询成品半成品物料
     *
     * @return
     */
    @PostMapping("/selectType")
    public Object selectType() {
        return success(managementService.selectType());
    }

    /**
     * 查询所有物料
     *
     * @return
     */
    @PostMapping("/selectAll")
    public Object selectAll(String companyId) {
        if(StringUtils.isBlank(companyId)){
            companyId = UserAuthUtil.getLoginUser().getCompanyId();
        }
        return success(managementService.selectAll(companyId));

    }

    /**
     * 根据id查询
     *
     * @param params
     * @return
     */
    @PostMapping("select")
    public Object select(@RequestBody JSONObject params) {
        String id = params.getString("id");
        if (id.length() > 0) {
            return success(managementService.selects(id));
        } else {
            return fail("数据错误");
        }
    }


    /**
     * 搜索栏
     *
     * @param params
     * @return
     */
    @PostMapping("/search")
    public Object search(@RequestBody JSONObject params) {
        String materialType = params.getString("materialType");
        String materialName = params.getString("materialName");
        if (materialType.equals("全部")) {
            return managementService.searchName(materialName);
        } else {
            if (StringUtils.isNotBlank(materialName)) {
                return managementService.search(materialType, materialName);
            } else {
                return managementService.searchType(materialType);
            }
        }
    }

    @PostMapping("/findByList")
    public Object findByList(@RequestBody MaterialManagement materialManagement) {
        if(StringUtils.isBlank(materialManagement.getCompanyId())){
            materialManagement.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return success(managementService.findByList(materialManagement));
    }

    @PostMapping("/findByListNoPage")
    public Object findByListNoPage(@RequestBody MaterialManagement materialManagement) {
        if(StringUtils.isBlank(materialManagement.getCompanyId())){
            materialManagement.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return success(managementService.findByListNoPage(materialManagement));
    }


    /**
     * 根据id修改物料管理
     *
     * @param params
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody JSONObject params) {
        MaterialManagement management = params.getJSONObject("materialManagement").toJavaObject(MaterialManagement.class);
        managementService.update(management);
        return success("修改成功");
    }

    /**
     * 根据id删除物料管理
     *
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody JSONObject params) {
        String id = params.getString("id");
        managementService.delete(id);
        return success("删除成功");
    }


    /**
     * 导入物料管理模板
     *
     * @param request
     */
    @RequestMapping("/import")
    public Object imports(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        managementService.imports(file, userId, companyId);
        return success("导入成功");
    }

    /**
     * 导出物料管理模板
     *
     * @param response
     */
    @RequestMapping("/efforts")
    public void efforts(HttpServletResponse response) {
        managementService.efforts(response);
    }

}
